﻿using DailyApp.WPF.DTOs;
using DailyApp.WPF.HttpClients;
using MaterialDesignThemes.Wpf;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using RestSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DailyApp.WPF.ViewModels
{
    /// <summary>
    /// 待办事项视图模型
    /// </summary>
    class WaitUCViewModel : BindableBase, IConfirmNavigationRequest
    {
        private readonly HttpRestClient httpRestClient;


        #region 委托命令

        /// <summary>
        /// 添加待办事项的命令
        /// </summary>
        public DelegateCommand ShowOrHidenRightMenuCmd { get; set; }


        /// <summary>
        /// 条件查询
        /// </summary>
        public DelegateCommand QueryWaitInfoListCmd { get; set; }


        /// <summary>
        /// 添加待办事项
        /// </summary>
        public DelegateCommand AddWaitInfoCmd { get; set; }


        /// <summary>
        /// 删除待办事项
        /// </summary>
        public DelegateCommand<WaitTodoInfoDTO> DeleteWaitInfoCmd { get; set; }



        #endregion


        #region 属性


        /// <summary>
        /// 待办事项的属性
        /// </summary>
        private List<WaitTodoInfoDTO> _waitTodoInfoList;
        public List<WaitTodoInfoDTO> WaitTodoInfoList
        {
            get
            {
                return _waitTodoInfoList;
            }
            set
            {
                _waitTodoInfoList = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 右侧弹窗添加待办事项
        /// </summary>
        private WaitTodoInfoDTO _addWaitInfo;
        public WaitTodoInfoDTO AddWaitInfo
        {
            get
            {
                if (_addWaitInfo == null)
                    _addWaitInfo = new WaitTodoInfoDTO();//在获取时就应该实例化，否则前端数据无法进行绑定
                return _addWaitInfo;
            }
            set
            {
                _addWaitInfo = value;
                RaisePropertyChanged();//其实单项绑定即可
            }
        }


        /// <summary>
        /// 添加待办事项菜单是否打开的属性
        /// </summary>
        private bool _isRightDrawerOpen = false;
        public bool IsRightDrawerOpen
        {
            get { return _isRightDrawerOpen; }
            set
            {
                _isRightDrawerOpen = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 条件查询待办事项标题
        /// </summary>
        private string _searchWaitTitle;
        public string SearchWaitTitle
        {
            get { return _searchWaitTitle; }
            set { _searchWaitTitle = value; }
        }


        /// <summary>
        /// 条件查询待办事项状态
        /// </summary>
        private int _searchWaitStatus = 2;
        public int SearchWaitStatus
        {
            get { return _searchWaitStatus; }
            set
            {
                _searchWaitStatus = value;
                QueryWaitInfoFunc();
                RaisePropertyChanged();//双向绑定，因为需在后端传递时更新前端页面选项
            }
        }


        #endregion


        /// <summary>
        /// 构造函数
        /// </summary>
        public WaitUCViewModel(HttpRestClient _httpRestClient)
        {
            httpRestClient = _httpRestClient;

            //委托命令实现
            ShowOrHidenRightMenuCmd = new DelegateCommand(ShowOrHidenRightMenuFunc);//弹窗
            QueryWaitInfoListCmd = new DelegateCommand(QueryWaitInfoFunc);//查询
            AddWaitInfoCmd = new DelegateCommand(AddWaitInfoFuncAsync);//添加待办事项
            DeleteWaitInfoCmd = new DelegateCommand<WaitTodoInfoDTO>(DeleteWaitInfoFunc);//删除待办事项

            //更新
            QueryWaitInfoFunc();
        }


        #region 具体方法


        /// <summary>
        /// 初始化待办事项
        /// </summary>
        private async void QueryWaitInfoFunc()
        {
            WaitTodoInfoList = new List<WaitTodoInfoDTO>();

            //请求Api
            //调用Api储存--更新
            ApiRequest apiRequest = new ApiRequest();
            apiRequest.Method = Method.GET;
            apiRequest.Route = $"Wait/QueryWaitInfo?title={SearchWaitTitle}&status={SearchWaitStatus}";


            var resp = await httpRestClient.ExecuteAsync(apiRequest);//请求Api

            //更新数据
            if (resp.ResultCode == 1)
            {
                WaitTodoInfoList = JsonConvert.DeserializeObject<List<WaitTodoInfoDTO>>(resp.ResultData.ToString());
            }
        }


        /// <summary>
        /// 弹出右侧添加待办事项窗体
        /// </summary>
        private void ShowOrHidenRightMenuFunc()
        {
            if (!IsRightDrawerOpen)
            {
                IsRightDrawerOpen = true;
            }
            else
                IsRightDrawerOpen = false;
        }


        /// <summary>
        /// 添加待办事项
        /// </summary>
        private async void AddWaitInfoFuncAsync()
        {
            ApiRequest apiRequest = new ApiRequest();

            //调用webapi
            apiRequest.Method = Method.POST;
            apiRequest.Route = "Wait/AddWaitInfo";
            apiRequest.Parameters = AddWaitInfo;

            var response = await httpRestClient.ExecuteAsync(apiRequest);

            if (response.ResultCode == 1)
            {
                QueryWaitInfoFunc();
                IsRightDrawerOpen = false;
                MessageBox.Show("添加成功!");
            }
            else
            {
                MessageBox.Show("添加失败!");
            }
        }


        /// <summary>
        /// 删除待办事项
        /// </summary>
        private async void DeleteWaitInfoFunc(WaitTodoInfoDTO delWaitInfo)
        {
            var apiRequest = new ApiRequest();
            apiRequest.Method = Method.DELETE;
            //apiRequest.Route = "Wait/DeleteWaitInfo"; ---error:delete请求一般不能直接传递参数
            apiRequest.Route = $"Wait/DeleteWaitInfo?delId={delWaitInfo.WaitId}";
            //apiRequest.Parameters = delWaitInfo;

            var res = await httpRestClient.ExecuteAsync(apiRequest);

            if (res.ResultCode == 1)
            {
                //更新
                QueryWaitInfoFunc();
                MessageBox.Show("删除成功！");
            }
            else
            {
                MessageBox.Show("服务器忙...");
            }
        }


        #endregion


        #region 接口实现

        public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
            //拦截是否导航
            continuationCallback?.Invoke(true);//必须为true，否则就被拦截了
        }


        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //根据由主页专递过来的参数进行条件查询
            if (navigationContext.Parameters.ContainsKey("SelectedIndex"))
            {
                SearchWaitStatus = navigationContext.Parameters.GetValue<int>("SelectedIndex");
            }
            else
            {
                SearchWaitStatus = 2;//当自己点自己即无传参行为的时候，就直接显示全部的  
            }
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }


        #endregion
    }
}
